package me.riddhimanadib.formmaster.helper;

import android.content.Context;
import android.support.v7.app.AppCompatDialog;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import me.riddhimanadib.formmaster.R;

/**
 * 单选加筛选Dialog
 */

public class SingleFilterDialog extends AppCompatDialog {

    public interface Callback {
        void onItemSelected(String value);
    }

    private static final String TAG = "SingleFilterDialog";

    private ListView lsvItems;
    private EditText edtKey = null;
    private ArrayAdapter<String> adapter = null;

    private List<String> filterKeys;
    private Callback callback;

    public SingleFilterDialog(Context context, List<String> items) {
        super(context);
        init(context, items);
    }

    public SingleFilterDialog(Context context, int theme, List<String> items) {
        super(context, theme);
        init(context, items);
    }

    protected SingleFilterDialog(Context context, boolean cancelable, OnCancelListener cancelListener, List<String> items) {
        super(context, cancelable, cancelListener);
        init(context, items);
    }

    public Callback getCallback() {
        return callback;
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public List<String> getFilterKeys() {
        return filterKeys;
    }

    public void setFilterKeys(List<String> filterKeys) {
        this.filterKeys = filterKeys;
    }

    private void init(Context context, List<String> items) {
        setContentView(R.layout.form_element_single_filter_dialog);
        edtKey = (EditText) findViewById(R.id.edtKey);
        edtKey.addTextChangedListener(filterTextWatcher);
        lsvItems = (ListView) findViewById(R.id.lsvItems);
        adapter = new ArrayAdapter<String>(context, items) {
            @Override
            public View createView(String item, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
                }
                ((TextView) convertView).setText(item);
                return convertView;
            }

            @Override
            protected boolean doFiltering(int position, String item, String constraintStr) {
                if (originalItems == null || originalItems.size() == 0) {
                    return true;
                } else {
                    if (filterKeys == null || filterKeys.size() == 0) {
                        return item.toLowerCase().contains(constraintStr.toLowerCase());
                    } else {
                        String key = filterKeys.get(position).toLowerCase();
                        return key.contains(constraintStr.toLowerCase());
                    }
                }
            }
        };
        lsvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (callback != null) {
                    String value = adapter.getItem(position);
                    callback.onItemSelected(value);
                    dismiss();
                }
            }
        });
        lsvItems.setAdapter(adapter);
    }

    private TextWatcher filterTextWatcher = new TextWatcher() {

        public void afterTextChanged(Editable s) {
            String key = s.toString().trim();
            adapter.beginFiltering(key);
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }
    };

    @Override
    public void onStop() {
        edtKey.removeTextChangedListener(filterTextWatcher);
    }

}
